package com.aimee.dao;

import com.aimee.entity.ExamPaperQuestion;
import com.aimee.entity.ExamRecord;
import com.aimee.util.Dbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ExamQuestionDAO {
    //********************************试卷内容***********************************************
    //根据考试id查询试卷内容
    public List findexamquestionsbyid(int exam_id) throws SQLException {
        String sql = "SELECT e.exam_id,e.exampaper_id,e.exam_name,e.exam_time_limit,ep.exampaper_score,ep.exampaper_passscore,q.question_id,q.question_content,q.question_score,q.question_type,q.question_optiona,q.question_optionb,q.question_optionc,q.question_optiond,q.question_answer\n" +
                "FROM `exampaper_qustion` eq\n" +
                "INNER JOIN `questions` q  ON eq.question_id=q.question_id\n" +
                "INNER JOIN `exampaper` ep ON eq.exampaper_id=ep.exampaper_id\n" +
                "INNER JOIN `exam` e ON ep.exampaper_id=e.exampaper_id\n" +
                "WHERE exam_id=?\n";
        Connection conn = Dbutil.getConn();
        QueryRunner runner = new QueryRunner();
        List list = runner.query(conn, sql, new BeanListHandler<ExamPaperQuestion>(ExamPaperQuestion.class), exam_id);
        conn.close();
        return list;
    }

    //根据id查询试卷问题数
    public Long countexamquestionbyid(int exam_id) throws SQLException {
        String sql = "SELECT COUNT(*)\n" +
                "FROM `exampaper_qustion` eq\n" +
                "INNER JOIN `questions` q  ON eq.question_id=q.question_id\n" +
                "INNER JOIN `exampaper` ep ON eq.exampaper_id=ep.exampaper_id\n" +
                "INNER JOIN `exam` e ON ep.exampaper_id=e.exampaper_id\n" +
                "WHERE exam_id=?";
        Connection conn = Dbutil.getConn();
        QueryRunner qr = new QueryRunner();
        long count = qr.query(conn, sql, new ScalarHandler<Long>(), exam_id);
        System.out.println(count);
        conn.close();
        return count;
    }



}
